package com.rz.admin.controller;

import com.rz.admin.entity.Admin;
import com.rz.admin.entity.vo.DeleteParam;
import com.rz.admin.service.IAdminRoleService;
import com.rz.admin.service.IAdminService;
import com.rz.admin.service.IRoleResService;
import com.rz.admin.service.IRoleService;
import com.rz.core.kit.RespKit;
import com.rz.core.resp.ResultResp;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin")
public class AdminCtrl {

    @Resource
    private IAdminService adminService;
    @Resource
    private IAdminRoleService adminRoleService;
    @Resource
    private IRoleResService roleResService;
    @Resource
    private IRoleService roleService;

    @PostMapping("login")
    public ResultResp login(@RequestBody Admin admin){
        return RespKit.success(adminService.login(admin));
    }

    @PostMapping("save")
    public ResultResp save(@RequestBody Admin admin){
        return RespKit.success(adminService.onSave(admin));
    }

    @PostMapping("page")
    public ResultResp page(@RequestBody Admin admin){
        return RespKit.success(adminService.getPage(admin));
    }

    @PostMapping("delete")
    public ResultResp delete(@RequestBody DeleteParam deleteParam){
        return RespKit.success(adminService.batchDelete(deleteParam));
    }

    @PostMapping("getAdminByType/{type}")
    public ResultResp getAdminByType(@PathVariable Integer type){
        return RespKit.success(adminService.getAdminByType(type));
    }

    @PostMapping("getResByAdminId/{adminId}")
    public ResultResp getResByAdminId(@PathVariable Integer adminId){
        return RespKit.success(roleResService.getResTreeByAdminId(adminId,true));
    }

    @PostMapping("getRolesByAdminId/{adminId}")
    public ResultResp getRolesByAdminId(@PathVariable Integer adminId){
        return RespKit.success(adminRoleService.getRolesByAdminId(adminId));
    }

    @PostMapping("getByCrteAdminId/{adminId}")
    public ResultResp getByCrteAdminId(@PathVariable Integer adminId){
        return RespKit.success(roleService.getByCrteAdminId(adminId));
    }

    @PostMapping("getRoleIdsByAdminId/{adminId}")
    public ResultResp getRoleIdsByAdminId(@PathVariable Integer adminId){
        return RespKit.success(adminRoleService.getRoleIdsByAdminId(adminId));
    }

    @PostMapping("getResListByAdminId/{adminId}")
    public ResultResp getResListByAdminId(@PathVariable Integer adminId){
        return RespKit.success(roleResService.getResByAdminId(adminId,true));
    }

    @PostMapping("updatePwd")
    public ResultResp updatePwd(@RequestBody Admin admin){
        return RespKit.success(adminService.updatePwd(admin));
    }

    @PostMapping("getResTreeByAdminId/{adminId}")
    public ResultResp getResTreeByAdminId(@PathVariable Integer adminId){
        Map<String,Object> map = new HashMap<>();
        map.put("resList",roleResService.getResByAdminId(adminId,true));
        map.put("resTreeList",roleResService.getResTreeByAdminId(adminId,false));
        return RespKit.success(map);
    }
}
